import Vue from 'vue'
import VueRouter from 'vue-router'
import Router from 'vue-router'
import Login from '../views/Login'
import Index from '../views/Index'
import Welcome from '../views/Welcome'
import Collect from '../views/book/collect/Collect'
import Hot from '../views/book/hot/Hot'
import Menu from '../views/sys/menu/Menu'
import Employee from '../views/sys/user/Employee'
import Customer from '../views/sys/user/Customer'
import Role from '../views/sys/role/Role'
import Permission from '../views/sys/permission/Permission'

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: "/login"
  },
  {
    path: "/login",
    component: Login
  },
  {
    path: "/index",
    component: Index,
    redirect: "/welcome",
    children: [
      {
        path: "/welcome",
        component: Welcome,
      },
      {
        path: "/book/hot",
        component: Hot,
      },
      {
        path: "/book/collect",
        component: Collect,
      },
      {
        path: "/sys/menu",
        component: Menu,
      },
      {
        path: "/sys/user/employee",
        component: Employee,
      },
      {
        path: "/sys/user/customer",
        component: Customer,
      },
      {
        path: "/sys/role",
        component: Role,
      },
      {
        path: "/sys/permission",
        component: Permission,
      },
    ]
  },
  
]

const router = new VueRouter({
  routes
})

// 路由异常处理
const originalPush = Router.prototype.push;
Router.prototype.push = function push(localtion, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalPush.call(this, localtion, onResolve, onReject);
  }
  return originalPush.call(this, localtion).catch(err => err);
}

// 挂载路由守卫
router.beforeEach((to, from, next) => {
  // 类似后端的filter
  if (to.path == "/login") {
    return next();
  }
  const token = window.sessionStorage.getItem("Authorization");
  if (!token) {
    return next("/login");
  }
  next();
})

export default router
